<template>
  <div class="login-page">
    <div class="login-container">
      <!-- 使用本地图片作为logo -->
      <div class="logo">
        <img src="@/assets/logo.png" alt="系统logo" />
      </div>
      <div class="title">登入智慧门禁账号</div>

      <el-form
        @keyup.enter="handleLogin(loginFormRef)"
        ref="loginFormRef"
        style="max-width: 600px"
        :model="loginForm"
        status-icon
        :rules="rules"
        label-width="auto"
        class="login-form"
      >
        <!-- 账号 -->
        <el-form-item label="" prop="username">
          <el-input
            placeholder="请输入手机号/会员名/邮箱"
            v-model="loginForm.username"
            type="text"
            autocomplete="off"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginForm.password"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="handleLogin(loginFormRef)">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { apiIsLogined } from '@/api/user'
import { UserState } from '@/stores/user'
import { type FormInstance, type FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const userState = UserState()

const route = useRoute()
const router = useRouter()

const loginFormRef = ref<FormInstance>()
const loginForm = reactive({
  username: '',
  password: '',
})

const rules = reactive<FormRules<typeof loginForm>>({
  username: [{ required: true, message: '请输入手机号/会员名/邮箱', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      // 登录逻辑
      console.log('登录请求参数：', loginForm)
      userState.login(loginForm.username, loginForm.password).then(() => toRedirect())
    } else {
      console.log('error submit!', fields)
    }
  })
}

const toRedirect = () => {
  if (userState.role === 0) {
    router.replace('/manage')
  } else {
    router.replace((route.query.redirect as string) || '/')
  }
}

onMounted(() => {
  apiIsLogined().then(() => toRedirect())
})
</script>

<style scoped>
.login-page {
  margin: 0;
  padding: 0;
  background-color: #007bff;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  font-family: Arial, sans-serif;
}

.login-container {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 350px;
  text-align: center;
}

/* 修改logo样式，适应本地图片 */
.logo {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
}

.logo img {
  width: 100%;
  height: 100%;
  object-fit: contain; /* 保持图片比例，适应容器 */
}

.title {
  font-size: 18px;
  color: #007bff;
  margin-bottom: 30px;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #666;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
}

.login-btn:hover {
  background-color: #0056b3;
}
</style>
